﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using ProjectHT.YKDGVB2012.Core.Untility;

namespace ProjectHT.YKDGVB2012.Core.Hanlder
{
    public class CapcharHandler : IHttpHandler, IRequiresSessionState 
    {

        #region Implementation of IHttpHandler
        public void ProcessRequest(HttpContext context)
        {

            var reqType = context.Request.RequestType;
            switch (reqType)
            {
                case "POST":
                    PostMethod(context);
                    break;
                case "GET":
                    GetMethod(context);
                    break;
            }
        }
        private void GetMethod(HttpContext context)
        {
            var method = context.Request.QueryString[Helper.Constans.MOD];
            if (String.IsNullOrEmpty(method)) return;
            switch (method)
            {
                case "GetCapcha":
                    GetCapcha(context);
                    break;
                case "ReloadCapcha":
                    ReloadCapcha(context);
                    break;
            }
        }

        private void ReloadCapcha(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            var captcha = new CatpchaImage();
            var str = captcha.DrawNumbers(5);
            if (context.Session[CatpchaImage.SESSION_CAPTCHA] == null)
            {
                context.Session.Add(CatpchaImage.SESSION_CAPTCHA, str);
            }
            else
            {
                context.Session[CatpchaImage.SESSION_CAPTCHA] = str;
            }
            var bmp = captcha.Result;
            bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

        }

        private void GetCapcha(HttpContext context)
        {
            context.Response.ContentType = "image/jpeg";
            var captcha = new CatpchaImage();
            var str = captcha.DrawNumbers(5);
            if (context.Session[CatpchaImage.SESSION_CAPTCHA] == null)
            {
                context.Session[CatpchaImage.SESSION_CAPTCHA] = str;
            }
            else
            {
                context.Session[CatpchaImage.SESSION_CAPTCHA] = str;
            }
            var bmp = captcha.Result;
            bmp.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        private void PostMethod(HttpContext context)
        {
            var method = context.Request.QueryString["method"];
            if (!String.IsNullOrEmpty(method))
            {
                switch (method)
                {
                    case "CCode":
                        CheckCode(context);
                        break;
                }


            }

        }

        private void CheckCode(HttpContext context)
        {
            var codeInput = context.Request.Form["Codecheck"];
            var flag = false;
            var textbuild = new StringBuilder();
            var jsEzaile = new JavaScriptSerializer();
            if (!String.IsNullOrEmpty(codeInput))
            {

                if (context.Session[CatpchaImage.SESSION_CAPTCHA].Equals(codeInput))
                {
                    var mess = new Common.Messager() { Statust = "true" };
                    jsEzaile.Serialize(mess, textbuild);
                    context.Response.Clear();
                    context.Response.ContentType = "application/json;utf-8";
                    context.Response.Write(textbuild.ToString());
                }
                else
                {
                    var mess = new Common.Messager() { Statust = "false" };
                    jsEzaile.Serialize(mess, textbuild);
                    context.Response.Clear();
                    context.Response.ContentType = "application/json;utf-8";
                    context.Response.Write(textbuild.ToString());
                }
            }
        }


        public bool IsReusable
        {
            get { return false; }
        }


        #endregion
    }

    
}
